#Requires AutoHotkey v2.0

class MyToolBar {
  static btnW := MyToolBar.MyButton.btnW
  static btnH := MyToolBar.MyButton.btnH

  __New(btnCount, nameList, posX, posY, CallBackList, btnW := unset, btnH := unset) {
    this.btnCount := btnCount
    this.nameList := nameList
    this.posX := posX
    this.posY := posY
    this.CallBackList := CallBackList
    IsSet(btnW) && MyToolBar.MyButton.btnW := btnW
    IsSet(btnH) && MyToolBar.MyButton.btnH := btnH
  }

  static SetWH(w, h) {
    MyToolBar.MyButton.btnW := w
    MyToolBar.MyButton.btnH := h
  }

  GetPos(&x, &y, &w, &h) {
    x := this.posX
    y := this.posY
    w := this.btnCount * MyToolBar.btnW
    h := MyToolBar.btnH
  }

  Modefiy(index, text) {
    this.btnList[index].Text := text
  }

  Show(x := unset, y := unset) {
    btnList := Array()
    IsSet(x) || x := this.posX
    IsSet(y) || y := this.posY
    this.posX := x
    this.posY := y
    loop this.btnCount {
      btnList.Push(MyToolBar.MyButton.Show(this.nameList[A_Index]
        , x + (A_Index - 1) * MyToolBar.MyButton.btnW
        , y, this.CallBackList[A_Index]))
    }
    this.btnList := btnList
  }

  Hide() {
    for v in this.btnList
      v.Hide()
  }

  Move(nx, ny) {
    this.posX := nx
    this.posY := ny
    loop this.btnCount {
      this.btnList[A_Index].Move(nx + MyToolBar.btnW * (A_Index - 1), ny)
    }
  }

  Destroy() {
    loop this.btnCount
      this.btnList[A_Index].Destroy()
  }

  Class MyButton extends Gui {
    static btnW := 70
    static btnH := 35
  
    __New(value) {
      super.__New('+AlwaysOnTop -Caption +ToolWindow')
      this.SetFont('s14')
      this.ins := this.AddButton('xs w' MyToolBar.MyButton.btnW ' h' MyToolBar.MyButton.btnH, value)
    }
  
    static Show(value, x, y, Action) {
      g := MyToolBar.MyButton(value)
      g.ins.OnEvent('Click', (*) => this.OnClickConfirm(g, Action))
      g.Show('x' x ' y' y ' w' MyToolBar.MyButton.btnW ' h' MyToolBar.MyButton.btnH)
      return g
    }
  
    static OnClickConfirm(ins, Callback) {
      Callback(ins)
    }
  }
}



if A_LineFile == A_ScriptFullPath {

  CB1(btn) {
    MsgBox A_ThisFunc '/' btn.ins.Text
  }
  CB2(btn) {
    MsgBox A_ThisFunc '/' btn.ins.Text
  }
  t := MyToolBar(2, ['Ok', 'Cancel'], 960, 700, [CB1, CB2])
  t.Show()
  ; t.Destroy()
}